package com.tangyee.home_blood_center.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.utils.DateUtil;
import com.tangyee.home_blood_center.utils.DensityUtils;
import com.tangyee.R;
import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.interfaces.OnBSRecordClickListener;
import com.tangyee.home_blood_center.utils.DateUtil;
import com.tangyee.home_blood_center.utils.DensityUtils;

/**
 * Blood Sugar Record Row
 * 和BPRecordRowView差不多
 */
public class BSRecordRowView extends ConstraintLayout {

    private View mView;
    private TextView mTextView0;
    private TextView mTextView1;
    private TextView mTextView2;
    private TextView mTextView3;
    private TextView mTextView4;
    private TextView mTextView5;
    private TextView mTextView6;
    private TextView mTextView7;
    private TextView mTextView8;

    private BSRecord mBSRecord;

    private OnBSRecordClickListener mListener;

    public BSRecordRowView(Context context) {
        super(context);
        initView(context);
    }

    public BSRecordRowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public BSRecordRowView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BSRecordRowView);
        initView(context);

        setText0(typedArray.getString(R.styleable.BSRecordRowView_cellText0));
        setText1(typedArray.getString(R.styleable.BSRecordRowView_cellText1));
        setText2(typedArray.getString(R.styleable.BSRecordRowView_cellText2));
        setText3(typedArray.getString(R.styleable.BSRecordRowView_cellText3));
        setText4(typedArray.getString(R.styleable.BSRecordRowView_cellText4));
        setText5(typedArray.getString(R.styleable.BSRecordRowView_cellText5));
        setText6(typedArray.getString(R.styleable.BSRecordRowView_cellText6));
        setText7(typedArray.getString(R.styleable.BSRecordRowView_cellText7));
        setText8(typedArray.getString(R.styleable.BSRecordRowView_cellText8));

        typedArray.recycle();

    }

    private void initView(Context context) {

        mView = LayoutInflater.from(context).inflate(R.layout.bs_record_row, this);
        mTextView0 = mView.findViewById(R.id.textView0);
        mTextView1 = mView.findViewById(R.id.textView1);
        mTextView1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.OnTextView1Click(mBSRecord);
            }
        });
        mTextView2 = mView.findViewById(R.id.textView2);
        mTextView2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.OnTextView2Click(mBSRecord);
            }
        });
        mTextView3 = mView.findViewById(R.id.textView3);
        mTextView3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.OnTextView3Click(mBSRecord);
            }
        });
        mTextView4 = mView.findViewById(R.id.textView4);
        mTextView4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.OnTextView4Click(mBSRecord);
            }
        });
        mTextView5 = mView.findViewById(R.id.textView5);
        mTextView5.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.OnTextView5Click(mBSRecord);
            }
        });
        mTextView6 = mView.findViewById(R.id.textView6);
        mTextView6.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.OnTextView6Click(mBSRecord);
            }
        });
        mTextView7 = mView.findViewById(R.id.textView7);
        mTextView7.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.OnTextView7Click(mBSRecord);
            }
        });
        mTextView8 = mView.findViewById(R.id.textView8);
        mTextView8.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.OnTextView8Click(mBSRecord);
            }
        });
    }

    private SpannableString formatSpannableString(String str) {
        if (TextUtils.isEmpty(str)){
            str = "1 1月";
        }
        SpannableString spannableString = new SpannableString(str);
        int t = str.indexOf(' ');
        int l = str.length();
        RelativeSizeSpan sizeSpan2 = new RelativeSizeSpan(0.65f);
//        SubscriptSpan subscriptSpan = new SubscriptSpan();
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#000000"));
        spannableString.setSpan(colorSpan, 0, t, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        spannableString.setSpan(sizeSpan2, t, l, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        spannableString.setSpan(subscriptSpan, t, l, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    private void formatString(TextView textView, String str) {
        if (str == null) {
            str = "0";
        }
        double d = Double.valueOf(str);
        if (d > 10) {
            textView.setTextColor(getResources().getColor(R.color.blood_center_red));
            textView.setText(str);
        } else if (d >= 4.4 && d <= 10.0) {
            textView.setTextColor(getResources().getColor(R.color.blood_center_green));
            textView.setText(str);
        } else if (d > 0 && d < 4.4){
            textView.setTextColor(getResources().getColor(R.color.blood_center_blue));
            textView.setText(str);
        } else {
            SpannableString spannableString = new SpannableString(" ");
            Drawable drawable = getResources().getDrawable(R.drawable.add);
            drawable.setBounds(0, 0, DensityUtils.dp2px(getContext(), 11), DensityUtils.dp2px(getContext(), 11));
            ImageSpan imageSpan = new ImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            textView.setText(spannableString);
        }
    }

    public void setText0(String text) {
        mTextView0.setText(formatSpannableString(text));
    }

    public void setText1(String text) {
        formatString(mTextView1, text);
    }

    public void setText2(String text) {
        formatString(mTextView2, text);
    }

    public void setText3(String text) {
        formatString(mTextView3, text);
    }

    public void setText4(String text) {
        formatString(mTextView4, text);
    }

    public void setText5(String text) {
        formatString(mTextView5, text);
    }

    public void setText6(String text) {
        formatString(mTextView6, text);
    }

    public void setText7(String text) {
        formatString(mTextView7, text);
    }

    public void setText8(String text) {
        formatString(mTextView8, text);
    }

    public void setBSRecord(BSRecord bsRecord) {
        this.mBSRecord = bsRecord;
        setText0(DateUtil.date2Str(mBSRecord.getDate(), "dd MM月"));
        setText1(String.valueOf(bsRecord.getDawn()));
        setText2(String.valueOf(bsRecord.getBeforeBreakfast()));
        setText3(String.valueOf(bsRecord.getAfterBreakfast()));
        setText4(String.valueOf(bsRecord.getBeforeLunch()));
        setText5(String.valueOf(bsRecord.getAfterLunch()));
        setText6(String.valueOf(bsRecord.getBeforeDinner()));
        setText7(String.valueOf(bsRecord.getAfterDinner()));
        setText8(String.valueOf(bsRecord.getBeforeSleep()));
        this.invalidate();
    }

    public void setOnBSRecordClickListener(OnBSRecordClickListener listener) {
        mListener = listener;
    }

    public BSRecord getBSRecord() {
        return mBSRecord;
    }
}
